package com.study.pdfutil.demos;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.study.pdfutil.demos.entity.InspectionReportDto;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * pdf导出工具
 */
public class pdfUtil {
    public static void main(String[] args) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String format = sdf.format(new Date());
        System.out.println("format = " + format);
        //创建文件
        Document document = new Document();
        //建立一个书写器
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\OneDrive\\桌面\\image\\"+format+".pdf"));
        //打开文件
        document.open();
        //中文字体,解决中文不能显示问题
        BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
        //蓝色字体
        Font defaultFont = new Font(bfChinese);
        defaultFont.setColor(BaseColor.BLACK);

        LinkedHashMap<Integer, List<InspectionReportDto>> data = getData();
        for(Map.Entry<Integer, List<InspectionReportDto>> map:data.entrySet()){
            List<InspectionReportDto> value = map.getValue();
            InspectionReportDto inspectionReportDto = value.get(0);
            //添加内容
            document.add(new Paragraph(inspectionReportDto.getInspectionBizName(),defaultFont));
            String inspectionIndicatorsJson = inspectionReportDto.getInspectionIndicatorsJson();
            JSONArray objects = JSONObject.parseArray(inspectionIndicatorsJson);
            // 3列的表.
            PdfPTable table = new PdfPTable(objects.size()+1);
            table.setWidthPercentage(100); // 宽度100%填充
            table.setSpacingBefore(10f); // 前间距
            table.setSpacingAfter(10f); // 后间距

            List<PdfPRow> listRow = table.getRows();
            //设置列宽
           /* float[] columnWidths = { 1f, 2f, 3f };
            table.setWidths(columnWidths);*/
            //行1
            PdfPCell cells1[]= new PdfPCell[objects.size()+1];
            PdfPRow row1 = new PdfPRow(cells1);
            //单元格
            cells1[0] = new PdfPCell(new Paragraph("节点名称",defaultFont));//单元格内容
            cells1[0].setBorderColor(BaseColor.BLUE);//边框验证
            cells1[0].setPaddingLeft(20);//左填充20
            cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
            cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中

            for(int i=1;i<=objects.size();i++){
                JSONObject jsonObject =(JSONObject) objects.get(i-1);
                cells1[i] = new PdfPCell(new Paragraph(jsonObject.getString("key"),defaultFont));
            }

            listRow.add(row1);

            for(InspectionReportDto cellValue:value){
                PdfPCell newCell[]= new PdfPCell[objects.size()+1];
                PdfPRow pdfPRow = new PdfPRow(newCell);
                newCell[0] = new PdfPCell(new Paragraph(cellValue.getInspectionBizName(),defaultFont));
                String nodeJson = cellValue.getInspectionIndicatorsJson();
                JSONArray nodeObject = JSONObject.parseArray(nodeJson);
                for(int i=1;i<=nodeObject.size();i++){
                    JSONObject jsonObject =(JSONObject) nodeObject.get(i-1);
                    newCell[i] = new PdfPCell(new Paragraph(jsonObject.getString("resultMsg"),defaultFont));
                }
                listRow.add(pdfPRow);
            }

            //把表格添加到文件中
            document.add(table);

        }

        //关闭文档
        document.close();
        //关闭书写器
        writer.close();

    }


    public static LinkedHashMap<Integer, List<InspectionReportDto>> getData(){
        List<InspectionReportDto> tableList = new ArrayList<>();
        for(int i=1;i<=2;i++){
            if(i%2==0){
                for(int j=2;j<=3;j++){
                    InspectionReportDto inspectionReportDto = new InspectionReportDto();
                    inspectionReportDto.setInspectionScriptId(i);
                    inspectionReportDto.setInspectionBizType(i);
                    inspectionReportDto.setInspectionBizName("节点"+j);
                    inspectionReportDto.setInspectionIndicatorsJson("[{\"key\":\"serviceStatus\",\"operateSymbol\":null,\"threshold\":0.0,\"unit\":null,\"category\":\"service\",\"describe\":\"服务状态\",\"resultCode\":0,\"resultMsg\":\"当前检测值:running\"},{\"key\":\"cpuUsage\",\"operateSymbol\":3,\"threshold\":75.0,\"unit\":\"%\",\"category\":\"cpu\",\"describe\":\"cpu使用率\",\"resultCode\":1,\"resultMsg\":\"当前检测值:47\"},{\"key\":\"memoryUsage\",\"operateSymbol\":3,\"threshold\":75.0,\"unit\":\"%\",\"category\":\"memory\",\"describe\":\"内存使用率\",\"resultCode\":2,\"resultMsg\":\"未检测到该指标对应的数据\"},{\"key\":\"diskUsage\",\"operateSymbol\":3,\"threshold\":75.0,\"unit\":\"%\",\"category\":\"disk\",\"describe\":\"硬盘使用率\",\"resultCode\":1,\"resultMsg\":\"当前检测值:13.10\"},{\"key\":\"networkRx\",\"operateSymbol\":null,\"threshold\":0.0,\"unit\":null,\"category\":\"network\",\"describe\":\"网络接收\",\"resultCode\":0,\"resultMsg\":\"当前检测值:0B\"},{\"key\":\"networkTx\",\"operateSymbol\":null,\"threshold\":0.0,\"unit\":null,\"category\":\"network\",\"describe\":\"网络发送\",\"resultCode\":0,\"resultMsg\":\"当前检测值:/\"}]");
                    tableList.add(inspectionReportDto);
                }
            }else{
                for(int j=2;j<=3;j++){
                    InspectionReportDto inspectionReportDto = new InspectionReportDto();
                    inspectionReportDto.setInspectionScriptId(i);
                    inspectionReportDto.setInspectionBizType(i);
                    inspectionReportDto.setInspectionBizName("服务"+j);
                    inspectionReportDto.setInspectionIndicatorsJson("[{\"key\":\"load5\",\"operateSymbol\":null,\"threshold\":0.0,\"unit\":null,\"category\":\"cpu\",\"describe\":\"过去5分钟的平均负载\",\"resultCode\":0,\"resultMsg\":\"当前检测值:4\"},{\"key\":\"cpuCores\",\"operateSymbol\":null,\"threshold\":0.0,\"unit\":null,\"category\":\"cpu\",\"describe\":\"cpu核数\",\"resultCode\":0,\"resultMsg\":\"当前检测值:4\"},{\"key\":\"cpuUsage\",\"operateSymbol\":3,\"threshold\":75.0,\"unit\":\"%\",\"category\":\"cpu\",\"describe\":\"cpu使用率\",\"resultCode\":1,\"resultMsg\":\"当前检测值:60.5\"},{\"key\":\"memoryTotal\",\"operateSymbol\":null,\"threshold\":0.0,\"unit\":null,\"category\":\"memory\",\"describe\":\"当前节点内存总量\",\"resultCode\":2,\"resultMsg\":\"未检测到该指标对应的数据\"},{\"key\":\"memoryUsage\",\"operateSymbol\":3,\"threshold\":75.0,\"unit\":\"%\",\"category\":\"memory\",\"describe\":\"内存使用率\",\"resultCode\":2,\"resultMsg\":\"未检测到该指标对应的数据\"},{\"key\":\"diskTotal\",\"operateSymbol\":null,\"threshold\":0.0,\"unit\":null,\"category\":null,\"describe\":\"当前节点根目录硬盘总量\",\"resultCode\":0,\"resultMsg\":\"当前检测值:45G\"},{\"key\":\"diskUsage\",\"operateSymbol\":3,\"threshold\":75.0,\"unit\":\"%\",\"category\":\"disk\",\"describe\":\"硬盘使用率\",\"resultCode\":1,\"resultMsg\":\"当前检测值:29\"},{\"key\":\"exceptionDevice\",\"operateSymbol\":null,\"threshold\":0.0,\"unit\":null,\"category\":\"network\",\"describe\":\"发现异常网卡(工作模式为PROMISC的网卡)\",\"resultCode\":2,\"resultMsg\":\"未检测到该指标对应的数据\"},{\"key\":\"firewallStatus\",\"operateSymbol\":null,\"threshold\":0.0,\"unit\":null,\"category\":\"network\",\"describe\":\"防火墙状态\",\"resultCode\":0,\"resultMsg\":\"当前检测值:inactive\"},{\"key\":\"latency\",\"operateSymbol\":3,\"threshold\":3000.0,\"unit\":\"ms\",\"category\":\"network\",\"describe\":\"节点到默认网关的网络延迟\",\"resultCode\":1,\"resultMsg\":\"当前检测值:1.801\"},{\"key\":\"packetLoss\",\"operateSymbol\":3,\"threshold\":10.0,\"unit\":\"%\",\"category\":\"network\",\"describe\":\"节点到默认网关网络的丢包率\",\"resultCode\":1,\"resultMsg\":\"当前检测值:0\"},{\"key\":\"bigFileInfo\",\"operateSymbol\":3,\"threshold\":500.0,\"unit\":\"MB\",\"category\":\"disk\",\"describe\":\"发现日志目录(/var/log)存在大文件\",\"resultCode\":2,\"resultMsg\":\"未检测到该指标对应的数据\"},{\"key\":\"sudoUser\",\"operateSymbol\":null,\"threshold\":0.0,\"unit\":null,\"category\":\"system\",\"describe\":\"拥有sudo权限的用户\",\"resultCode\":0,\"resultMsg\":\"当前检测值:koal\"},{\"key\":\"emptyPasswordUser\",\"operateSymbol\":null,\"threshold\":0.0,\"unit\":null,\"category\":\"system\",\"describe\":\"未设置密码的用户\",\"resultCode\":2,\"resultMsg\":\"未检测到该指标对应的数据\"}]");
                    tableList.add(inspectionReportDto);
                }
            }

        }

        LinkedHashMap<Integer, List<InspectionReportDto>> targetMap = tableList.stream().collect(Collectors.groupingBy(inspectionReportDto -> inspectionReportDto.getInspectionScriptId(), LinkedHashMap::new, Collectors.toList()));
        System.out.println("targetMap = " + targetMap);
        return targetMap;
    }
}
